/*
 * Copyright 2011 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.swf.tools.abcdump
{
	import flash.utils.ByteArray;
	import sweezy.swf.ITag;
	import sweezy.swf.SWF;
	import sweezy.swf.abc.ABC;
	import sweezy.swf.structure.ABCData;
	import sweezy.swf.tag.DoABC;
	import sweezy.swf.tag.DoABC2;

	public class ABCDump
	{

		public static function dumpABC(abc:ABC, stream:IDumpStream):void
		{
			if (abc !== null)
			{
				new ABCcontainerDef(abc).abcdump(stream);
			}
		}

		public static function dumpABCBytes(bytes:ByteArray, stream:IDumpStream):void
		{
			if (bytes !== null)
			{
				var abc:ABC = new ABC();
				abc.decode(bytes);
				dumpABC(abc, stream);
			}
		}

		public static function dumpABCData(abcData:ABCData, stream:IDumpStream):void
		{
			if (abcData !== null)
			{
				dumpABCBytes(abcData.data, stream);
			}
		}

		public static function dumpDoABC(doABC:DoABC, stream:IDumpStream):void
		{
			if (doABC !== null)
			{
				dumpABCData(doABC.abcData, stream);
			}
		}

		public static function dumpDoABC2(doABC:DoABC2, stream:IDumpStream):void
		{
			if (doABC !== null)
			{
				dumpABCData(doABC.abcData, stream);
			}
		}

		public static function dumpSWF(swf:SWF, stream:IDumpStream):void
		{
			if (swf !== null)
			{
				for each (var tag:ITag in swf.tags)
				{
					if (tag is DoABC)
					{
						dumpDoABC(tag as DoABC, stream);
					}
					else if (tag is DoABC2)
					{
						dumpDoABC2(tag as DoABC2, stream);
					}
				}
			}
		}

		public static function dumpSWFBytes(bytes:ByteArray, stream:IDumpStream):void
		{
			if (bytes !== null)
			{
				var swf:SWF = new SWF();
				swf.decode(bytes);
				dumpSWF(swf, stream);
			}
		}
	}
}
